from PIL import Image, ImageFont, ImageDraw, ImageFilter
import random


def rndColor1():
    return (random.randint(125, 255), random.randint(0, 125), random.randint(125, 255))


def rndColor2():
    return random.randint(0, 125)

 #生成验证码图片
 #length 验证码长度
 #width 图片宽度
 #height 图片高度
 #返回验证码和图片
def veri_code(lenght=4,width=160,height=40):
    #创建Image对象
    image = Image.new('RGB',(width,height),(255,255,255))
    #创建Font对象
    font = ImageFont.truetype('Arial.ttf',32)
    #创建Draw对象
    draw = ImageDraw.Draw(image)
    #随机颜色填充每个像素
    for x in range(width):
        for y in range(height):
            draw.point((x,y),fill=random.randint(64,255))
    #验证码
    code = random_code(lenght)
    #随机颜色验证码写到图片上
    for t in range(lenght):
        draw.text((40*t+5,5),code[t],font=font,fill=random.randint(32,127))
    #模糊滤镜
    image = image.filter(ImageFilter.BLUR)
    return code,image